define(function (require, exports, module) {
    'use strict';
    var Backbone = require('backbone'),
        cookie = require('js/utils/cookie'),
        $ = require('jquery'),
        notify = require('js/utils/notify'),
        utils = require('js/utils/tools'),
        _ = require('underscore');

    var MainRouter = Backbone.Router.extend({
        initialize: function () {
            var userId = cookie.getCookie('bigbang_user_id');
            if (!userId) {
                window.location.href = '/user.html';
                return;
            }
            if (userId && USER_INFO.info == null) {
                $.get('/admin/user/' + userId + '/').done(function (resp) {
                    $('#user-full-name').text(resp.data.nickname);
                    var role = resp.data.role;
                    var perm_role = resp.data.perm;
                    var perm_role_attrs = perm_role.split('|');
                    USER_INFO.info = resp.data;
                    if (role <= 2) {
                        $('#statsMenu').hide();
                        $('#reportMenu').hide();
                    }
                    if (role <= 1) {
                        $('#consoleMenu').hide();
                    }
                    // permission role init
                    _.each($("li"), function (ele){
                        var menu_id = ele.id;
                        if (menu_id && menu_id.startsWith("menu_")) {
                            var menu_attrs = menu_id.split('_');
                            if(menu_attrs.length > 2){
                                for(var num_i = 2; num_i<menu_attrs.length; num_i++){
                                    var menu_val = parseInt(menu_attrs[num_i]);
                                    var flag = false;
                                    for (var i = 0; i < perm_role_attrs.length; i++) {
                                        if (menu_val == perm_role_attrs[i]) {
                                            flag = true;
                                            break;
                                        }
                                    }
                                    if (flag) {
                                        $('#' + menu_id).show();
                                        var parent = $('#' + menu_id).parents("li").eq(0);
                                        parent.show();
                                        break;
                                    } else {
                                        $('#'+menu_id).hide();
                                    }
                                }
                            }
                        }
                    });
                }).fail(function (data) {
                    notify.notifyResp(data);
                    window.location.href = '/user.html';
                });
            }
            $('body').on('keypress', '#sidebar_right', function (e) {
                e.stopImmediatePropagation();
                if (e.which == 13) $('#search').click();
            });
            $.ajaxSetup({
                cache: false
            });
        },
    });

    exports.init = function () {
        var routes = [];
        _.each(['op', 'preset', 'stats', 'vips', 'console', 'report'], function (k) {
            routes.push('js/apps/' + k + '/router');
        });
        require(routes, function () {
            _.each(arguments, function (r) {
                new r.Router();
            });
            new MainRouter();
            Backbone.history.start();
        });
    };
});
